package jp.co.dailyReport.project.action;

import java.io.IOException;
import java.util.Vector;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.validator.DynaValidatorForm;
import jp.co.dailyReport.DataUnavailableException;
import jp.co.dailyReport.project.Project;
import jp.co.dailyReport.project.ProjectManager;
import jp.co.dailyReport.project.PjStaff;
import jp.co.dailyReport.project.PjStaffManager;
import jp.co.dailyReport.staff.Staff;
import jp.co.dailyReport.staff.StaffManager;
import jp.co.dailyReport.workItem.WorkItem;
import jp.co.dailyReport.workItem.WorkItemManager;
import jp.co.dailyReport.customer.Customer;
import jp.co.dailyReport.customer.CustomerManager;
import jp.co.dailyReport.SystemCalendar;

/**
 * ﾌﾟﾛｼﾞｪｸﾄ情報一覧ページのボタン（戻る、追加、編集、削除）
 * が押されたときの処理を管理するクラスです。
 */
public class ProjectAction extends Action{

	private static Logger log = Logger.getLogger(ProjectAction.class);

	/**
	 * ﾌﾟﾛｼﾞｪｸﾄ情報一覧ページで押されるボタン（戻る、追加、編集、削除）
	 * によって、実行する処理を制御します。<BR><BR>
	 * 「追加」ボタンが押された場合の処理：<BR>
	 * DoProjectActionFormオブジェクトをセッションから削除します。
	 * 取引先情報、社員情報を取得します。<BR><BR>
	 * 「編集」ボタンが押された場合の処理：<BR>
	 * プロジェクトIDからプロジェクト情報を取得し、DoProjectActionFormオブジェクトにセットします。<BR>
	 * 取引先情報、社員情報、プロジェクトIDからプロジェクトメンバー情報、プロジェクトIDから作業項目情報を取得します。<BR><BR>
	 * 「削除」ボタンが押された場合の処理：<BR>
	 * プロジェクトIDからプロジェクト情報を取得し、DoProjectActionFormオブジェクトにセットします。<BR>
	 * プロジェクトIDから作業項目情報を取得します。<BR>
	 * プロジェクトIDからプロジェクトメンバー情報を取得します。<BR>
	 * 取引先IDから取引先情報を取得します。<BR>
	 * プロジェクトリーダーの社員IDから社員情報を取得します。<BR>
	 * 
	 * @param mapping このインスタンスを選択するために使用したActionMapping
	 * @param form 存在するならば、このリクエストのためのActionForm Bean
	 * @param request 処理しているHTTPリクエスト
	 * @param response 処理しているHTTPレスポンス
	 * @return ActionForwardインスタンス
	 * @throws IOException, ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	 throws IOException, ServletException {

		log.debug("ProjectAction start");
		HttpSession session = request.getSession();
		log.debug("session.getId() = " + session.getId());
		String target = null;

		if(request.getParameter("add") != null) {
			target = "add";
			projectAdd(form, request);
		} else if(request.getParameter("edit") != null) {
			target = "edit";
			projectEdit(form, request);
		} else if(request.getParameter("rm") != null) {
			target = "rm";
			projectRm(form, request);
		} else if(request.getParameter("returnTop") != null) {
			target = "returnTop";
		}

		log.debug("ProjectAction end");
		return mapping.findForward(target);
	}

	private void projectAdd(ActionForm form, HttpServletRequest request) {

		HttpSession session = request.getSession();
		session.removeAttribute("DoProjectActionForm");
		Customer[] customerList = null;
		Staff[] staffList = null;
		Vector pjStaffList = new Vector();
		Vector pjWorkItemList = new Vector();
		DynaValidatorForm doProjectActionForm = (DynaValidatorForm)form;
		Calendar cal = Calendar.getInstance();

		doProjectActionForm.set("periodStartY", new Integer(cal.get(Calendar.YEAR)));
		doProjectActionForm.set("periodStartM", new Integer(cal.get(Calendar.MONTH)+1));
		doProjectActionForm.set("periodStartD", new Integer(cal.get(Calendar.DATE)));
		doProjectActionForm.set("periodEndY", new Integer(cal.get(Calendar.YEAR)));
		doProjectActionForm.set("periodEndM", new Integer(cal.get(Calendar.MONTH)+1));
		doProjectActionForm.set("periodEndD", new Integer(cal.get(Calendar.DATE)));
		try {
			customerList = CustomerManager.load();
			staffList = StaffManager.load();
		} catch(DataUnavailableException due) {
			log.error(due.getMessage());
		}
		session.setAttribute("pjStaffList", pjStaffList);
		session.setAttribute("pjWorkItemList", pjWorkItemList);
		request.setAttribute("customerList", customerList);
		request.setAttribute("staffList", staffList);
		request.setAttribute("staff", new Staff());
		request.setAttribute("systemCalendar", new SystemCalendar());
	}

	private void projectEdit(ActionForm form, HttpServletRequest request) {

		HttpSession session = request.getSession();
		Customer[] customerList = null;
		Staff[] staffList = null;
		Project project = null;
		Vector pjStaffList = new Vector();
		Vector rmPjStaffList = new Vector();
		Vector pjWorkItemList = new Vector();
		DynaValidatorForm doProjectActionForm = (DynaValidatorForm)form;
		int projectId = ((Integer)doProjectActionForm.get("projectId")).intValue();

		try {
			project = ProjectManager.load(projectId);
			doProjectActionForm.set("customerId", new Integer(project.getCustomerId()));
			doProjectActionForm.set("name", project.getName());
			doProjectActionForm.set("periodStartY", new Integer(project.getPeriodStart().get(Calendar.YEAR)));
			doProjectActionForm.set("periodStartM", new Integer(project.getPeriodStart().get(Calendar.MONTH)+1));
			doProjectActionForm.set("periodStartD", new Integer(project.getPeriodStart().get(Calendar.DATE)));
			doProjectActionForm.set("periodEndY", new Integer(project.getPeriodEnd().get(Calendar.YEAR)));
			doProjectActionForm.set("periodEndM", new Integer(project.getPeriodEnd().get(Calendar.MONTH)+1));
			doProjectActionForm.set("periodEndD", new Integer(project.getPeriodEnd().get(Calendar.DATE)));
			doProjectActionForm.set("pLStaffId", new Integer(project.getPLStaffId()));
			doProjectActionForm.set("outline", project.getOutline());
			doProjectActionForm.set("pjCustomerStaff", project.getPjCustomerStaff());
			doProjectActionForm.set("statusFlg", String.valueOf(project.isStatusFlg()));
			customerList = CustomerManager.load();

			staffList = StaffManager.load();

			PjStaff[] pjStaff = PjStaffManager.getStaffId(projectId);
			for(int i=0;i<pjStaff.length;i++) {
				pjStaffList.add(StaffManager.load(pjStaff[i].getStaffId()));
			}
			for(int i=0;i<pjStaff.length;i++) {
				rmPjStaffList.add(pjStaff[i]);
			}
			WorkItem[] workItem = WorkItemManager.loadPId(projectId);
			for(int i=0;i<workItem.length;i++) {
				pjWorkItemList.add(workItem[i]);
			}
		} catch(DataUnavailableException due) {
			log.error(due.getMessage());
		}
		session.setAttribute("pjStaffList", pjStaffList);
		session.setAttribute("pjWorkItemList", pjWorkItemList);

		session.setAttribute("rmPjStaffList", rmPjStaffList);
		session.setAttribute("rmPjWorkItemList", pjWorkItemList);

		request.setAttribute("customerList", customerList);
		request.setAttribute("staffList", staffList);
		request.setAttribute("staff", new Staff());
		request.setAttribute("systemCalendar", new SystemCalendar());
	}

	private void projectRm(ActionForm form, HttpServletRequest request) {

		HttpSession session = request.getSession();
		Project project = null;
		Customer customer = null;
		Staff pLStaff = null;
		Vector pjStaffList = new Vector();
		Vector pjWorkItemList = new Vector();
		DynaValidatorForm doProjectActionForm = (DynaValidatorForm)form;
		int projectId = ((Integer)doProjectActionForm.get("projectId")).intValue();

		try {
			project = ProjectManager.load(projectId);

			doProjectActionForm.set("customerId", new Integer(project.getCustomerId()));
			doProjectActionForm.set("name", project.getName());
			doProjectActionForm.set("periodStartY", new Integer(project.getPeriodStart().get(Calendar.YEAR)));
			doProjectActionForm.set("periodStartM", new Integer(project.getPeriodStart().get(Calendar.MONTH)+1));
			doProjectActionForm.set("periodStartD", new Integer(project.getPeriodStart().get(Calendar.DATE)));
			doProjectActionForm.set("periodEndY", new Integer(project.getPeriodEnd().get(Calendar.YEAR)));
			doProjectActionForm.set("periodEndM", new Integer(project.getPeriodEnd().get(Calendar.MONTH)+1));
			doProjectActionForm.set("periodEndD", new Integer(project.getPeriodEnd().get(Calendar.DATE)));
			doProjectActionForm.set("pLStaffId", new Integer(project.getPLStaffId()));
			doProjectActionForm.set("outline", project.getOutline());
			doProjectActionForm.set("pjCustomerStaff", project.getPjCustomerStaff());
			doProjectActionForm.set("statusFlg", String.valueOf(project.isStatusFlg()));

			customer = CustomerManager.load(project.getCustomerId());
			pLStaff = StaffManager.load(project.getPLStaffId());

			PjStaff[] pjStaff = PjStaffManager.getStaffId(projectId);
			for(int i=0;i<pjStaff.length;i++) {
				pjStaffList.add(StaffManager.load(pjStaff[i].getStaffId()));
			}
			WorkItem[] workItem = WorkItemManager.loadPId(projectId);
			for(int i=0;i<workItem.length;i++) {
				pjWorkItemList.add(workItem[i]);
			}
			request.setAttribute("staffList", StaffManager.load());
		} catch(DataUnavailableException due) {
			log.error(due.getMessage());
		}
		session.setAttribute("pjStaffList", pjStaffList);
		session.setAttribute("pjWorkItemList", pjWorkItemList);
		request.setAttribute("customer", customer);
		request.setAttribute("pLStaff", pLStaff);
	}
}
